﻿package  PinballCode{
	
	import flash.display.Shape;
	
	// Kicker object
	public class Kicker extends Shape {

		
		private var rad1:Number;			// radius 1 for kicker
		private var rad2:Number;			// radius 2 for kicker
		private var color1:uint;			// color 1 for kicker
		private var color2:uint;			// color 1 for kicker
		private var vertices:Array;			// list of vertices of the obj
		private var startVertex:Vertex;		// start vertex
		private var currentVertex:Vertex;	// current vertex
		private var center:Vertex;			// center vertex
		var maxY:Number;			// the highest height the ball can be at for this kicker and get points for the collision
		var minY:Number;			// the lowest height the ball can be at for this kicker and get points for the collision
		public var borderThickness:Number;	// thickness of border
		var theta:Number;					// theta of current angle degrees
		var rads:Number;					// angle in radians
//<<<<<<< .mine
		public var walls:Array;	
//=======
		var currentY:Number;				//calculated to set max and min Y
		private var widt:Number;
		private var ht:Number;
//>>>>>>> .r28
		
		//constructor
		public function Kicker(center:Vertex, rad1:Number=16, rad2:Number = 10, borderThickness = 1.5, color1:uint =0x000000, color2:uint=0x00FF00) {
			this.center = center;
			this.rad1 = rad1;
			this.rad2 = rad2;
			this.borderThickness = borderThickness;
			this.color1 = color1;
			this.color2 = color2;
			vertices = new Array();
//<<<<<<< .mine
			walls = new Array();
//=======
			maxY=0;
			minY=0;
//>>>>>>> .r28
			init();
		}
		
		// initalize kicker
		public function init():void {
			makeVertices();
			renderVertices();
		}
		
		public function getMin():Number {
			return minY;
		}
		public function getMax():Number {
			return maxY;
		}
		// makes the vertices
		public function makeVertices():void {
			
			// adds vertices like the polygon did, but it shortens the radius for 2 of the vertices, and we know there are only 4
			
			for(var x:Number = 1; x<= 4; x++)
			{
				//change the angle and turn it into radians
				theta= x * 90;
				rads = theta * Math.PI / 180;
				
				//the first 2 vertices are added with the larger radius, and the second 2 are added with the smaller radius.
				//other than that, this draws a regular rectangle so that the shape is a trapezoid
				switch(x)  //each iteration, a new vertex is added (in order) for the kicker
				{
					case 1:
						rads = (15 * Math.PI)/180;
						minY = center.y+rad1*Math.sin(rads);                                   //the first vertex will be the highest the Y can be
						vertices.push(new Vertex(center.x+rad1*Math.cos(rads), minY));
						break;
					case 2:
						rads = (105 * Math.PI)/180;
						maxY = center.y+rad1*Math.sin(rads);
						vertices.push(new Vertex(center.x+rad1*Math.cos(rads),maxY));
						walls.push(new Wall(vertices[0],vertices[1]));
						break;
					case 3:
						rads = (205 * Math.PI)/180;
						vertices.push(new Vertex(center.x+rad2*Math.cos(rads),center.y+rad2*Math.sin(rads)));
						walls.push(new Wall(vertices[1],vertices[2]));
						break;
					case 4:
						rads = 275 * Math.PI/180;
						vertices.push(new Vertex(center.x+rad2*Math.cos(rads),center.y+rad2*Math.sin(rads)));
						walls.push(new Wall(vertices[2],vertices[3]));
						walls.push(new Wall(vertices[3],vertices[0]));
						break;
				}
				
			}
			
		}
		
		// renders the vertices
		public function renderVertices():void {
			
			graphics.clear();
			
			graphics.beginFill(color2, alpha=1.0);
			
			graphics.lineStyle(borderThickness, color1, alpha=1.0);
			
			startVertex = vertices[0];
			graphics.moveTo(startVertex.x, startVertex.y);
			
			for (var x:Number = 1; x < vertices.length; x++)
			{
				currentVertex = vertices[x];                        //set the current vertex to the next on the array
				graphics.lineTo(currentVertex.x, currentVertex.y);  //draw the line to that vertex
				//graphics.moveTo(currentVertex.x, currentVertex.y);  //move to that vertex, to draw the next line from there
			}
			
			graphics.lineTo(startVertex.x, startVertex.y);      //draw line to complete the shape
			
			graphics.endFill();
		}
		
		public function scale(sx:Number,sy:Number):void {
			// must be along coordinate axes
			var angle:Number=Math.atan2(vertices[1].y-vertices[0].y,
								        vertices[1].x-vertices[0].x);
			
			rotate(-angle,false); 

			for (var i:int=0; i < vertices.length; i++) {
				vertices[i].x -= center.x;
				vertices[i].y -= center.y
			}
			
			for (var j:int=0; j < vertices.length; j++) {
				vertices[j].x *= sx;
				vertices[j].y *= sy;
			}
			
			widt = vertices[1].x-vertices[0].x;
			ht = vertices[3].y-vertices[0].y;
			
			for (var k:int=0; k < vertices.length; k++) {
				vertices[k].x += center.x;
				vertices[k].y += center.y;
			}
			
			center.x = vertices[0].x + widt/2;
			center.y = vertices[0].y + ht/2;
			
			this.rotate(angle,false); // rotate back
			
			init();
		}
		
		public function rotate(angle:Number,degrees:Boolean):void {
			for (var i:int=0; i < vertices.length; i++) {
				vertices[i].x -= center.x;
				vertices[i].y -= center.y;
			}
			
			for (var j:int=0; j < vertices.length; j++) {
				vertices[j] = Vertex.rotate(angle,degrees,vertices[j]);
			}
			
			for (var k:int=0; k < vertices.length; k++) {
				vertices[k].x += center.x;
				vertices[k].y += center.y;
			}
			
			//update();

		}
	}
	
}
